package com.itheima.dubbo.mysql.impl;


import com.alibaba.dubbo.config.annotation.Service;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.commons.pojo.entity.VideoInfo;
import com.itheima.dubbo.interfaces.VideoInfoApi;
import com.itheima.dubbo.mysql.mapper.VideoInfoMapper;
import org.springframework.beans.factory.annotation.Autowired;


import java.util.List;

/**
 * @author wds
 * @date 2021/12/20
 * @apiNote
 */
@Service
public class VideoInfoApiImpl implements VideoInfoApi {

    @Autowired
    VideoInfoMapper videoInfoMapper;


    @Override
    public void saveVideoInfo(VideoInfo videoInfo) {
        videoInfoMapper.insert(videoInfo);
    }

    @Override
    public List<VideoInfo> queryVideoInfo(Long uid, String sortProp, String sortOrder, Integer page, Integer pagesize) {
        LambdaQueryWrapper<VideoInfo> queryWrapper = Wrappers.lambdaQuery(VideoInfo.class)
                .eq(VideoInfo::getUserId,uid);

        switch (sortProp) {
            case "likeCount":
                queryWrapper = queryWrapper.orderBy(true, "ascending".equals(sortOrder), VideoInfo::getLikeCount);
                break;
            case "commentCount":
                queryWrapper = queryWrapper.orderBy(true, "ascending".equals(sortOrder), VideoInfo::getCommentCount);
                break;
            default:
                queryWrapper = queryWrapper.orderBy(true, "ascending".equals(sortOrder), VideoInfo::getCreated);
                break;
        }



        Page<VideoInfo> infoPage = videoInfoMapper.selectPage(new Page<>(page, pagesize),queryWrapper);

        return infoPage.getRecords();
    }
}
